package com.sunshine.lnuplus.view.Timetable;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.lifecycle.ViewModelProviders;
import androidx.viewpager.widget.ViewPager;

import com.sunshine.lnuplus.R;
import com.sunshine.lnuplus.view.Main.MainActivity;

import java.util.List;

public class TimetableFragment extends Fragment {

    private static TimetableViewModel mViewModel;
    private ViewPager viewPager;
    private MainActivity activity;
    private int thisWeek;
    private static int weeksCount;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        activity = (MainActivity) getActivity();
        setHasOptionsMenu(true);//创建菜单栏
        return inflater.inflate(R.layout.fragment_timetable, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mViewModel = ViewModelProviders.of(this).get(TimetableViewModel.class);

        thisWeek = mViewModel.getThisWeek();
        weeksCount = mViewModel.getWeeksCount();
        activity.getSupportActionBar().setTitle(mViewModel.getWeather());
        if(thisWeek==-1) activity.getSupportActionBar().setSubtitle("假期中");
        else activity.getSupportActionBar().setSubtitle("第"+thisWeek+"周");

        viewPager = getView().findViewById(R.id.timetable_view_pager);
        viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));//注意用getChildFragmentManager否则可能会view not found
        viewPager.setCurrentItem(thisWeek-1);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { }

            @Override
            public void onPageSelected(int position) {
                activity.getSupportActionBar().setSubtitle("第"+(position+1)+"周");
            }

            @Override
            public void onPageScrollStateChanged(int state) { }
        });
    }

    @Override
    public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        for(int count = 1;count<=weeksCount;count++)
            menu.add(0,count,0,"第"+count+"周");
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {

        for(int count = 1;count<=weeksCount;count++){
            if(item.getItemId()==count) viewPager.setCurrentItem(count-1);
        }
        return true;
    }

    public static List<List<String>> getTimetable(){
        return mViewModel.getTimetableFromDB();
    }

    static class MyAdapter extends FragmentStatePagerAdapter {

        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return EveryWeekTimetableFragment.newInstance(position+1);
        }

        @Override
        public int getCount() {
            return weeksCount;
        }
    }
}
